package com.apt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.apt.common.AptException;
import com.apt.common.DBUtility;
import com.apt.domain.UserTest;


public class UserTestDao extends BaseConnection {
	
	public UserTestDao(Connection conn)
	{
		super(conn);
		
		
	}
	
	public void insertIntoUserTest(List<UserTest> userAnswersList)
			throws AptException {
		try {

			PreparedStatement ps = connection
					.prepareStatement("INSERT INTO USER_TEST(USER_TEST_EVAL_ID,QUESTION_ID,ANSWER_ID,ANSWER) VALUES(?,?,?,?)");
			for (UserTest each : userAnswersList) {

				ps.setInt(1, each.getUserTestEvalId());
				ps.setInt(2, each.getQuestionId());
				ps.setInt(3, each.getAnswerId());
				ps.setString(4, each.getOption());
				ps.addBatch();

			}
			ps.executeBatch();
			ps.close();
			connection.commit();

		} catch (SQLException sqlEx) {
			sqlEx.printStackTrace();
			throw new AptException(
					"Unable to insert the test results for user test evaluation id :"
							+ ((UserTest) userAnswersList.get(0))
									.getUserTestEvalId());

		} finally {

			DBUtility.cleanUp(connection);

		}

	}

}
